<?php
require_once("../../../config.inc.php");
$isReloadable = true;
require_once("../../../app/_inc/include.php");
$errMsg = "";

if($aDataConPar["dbcType"] == "DB2"){
	require_once("syncDB2.class.php");
}elseif(strtoupper($aDataConPar["dbcType"]) == "MYSQL" || strtoupper($aDataConPar["dbcType"]) == "MYSQLI"){
	require_once("syncMYSQL.class.php");
}else{
	$errMsg = _t("DB type")." '".$aMetaConPar["dbcType"]."' "._t("not allowed.");;
}
if ($errMsg == "") {
	if (sxREQUEST('reqtype')  == 'syn' || sxREQUEST('reqtype') == 'cmp') {
		$objUser->setHistoryVar(array('reqtype'=>''));
		$aOpt = array('reqtype'=>sxREQUEST('reqtype'));
		$objSync = new syncro($aDataConPar, $aMetaConPar, $aOpt);
		if($objSync->errno == 0){
			if (count($objSync->aEntCreated) == 0 
			and count($objSync->aEntAltered) == 0
			and count($objSync->aEntDeleted) == 0){
				$errMsg = _t("Data-Base synchronized !")."\n".$objSync->error;
			} elseif (sxREQUEST('reqtype')  == 'syn') {
				if ((count($objSync->aEntCreated)+count($objSync->aEntAltered)) > 0 ) {
					$errMsg = _t("DB Synchro completed")."\n".$objSync->error;
				} else {
					$errMsg = _t("No synchro")."\n".$objSync->error;
				}
			} else {
				$errMsg = _t("DB not synchronized")."\n".$objSync->error;
			}			
		}else{
			$errMsg = _t("Errors found")."\n".$objSync->error;
		}
	}
}

//output HTML
$aOpz = array("kMenu"=>'dam.sdb', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
//commands
$aCmd = array(
					'cmp'=>array("pag"=>"syncDB.php?reqtype=cmp",
										"title"=>_t("compare"),
										"text"=>_t("compare")),
					'syn'=>array("pag"=>"syncDB.php?reqtype=syn",
										"title"=>_t("DB synchro"),
										"text"=>_t("DB synchro")),
					"back"=>array("pag"=>$objUser->getHistory(-1,false),
										"title"=>$objUser->getHistoryText(-1),
										"text"=>$objUser->getHistoryText(-1))
					);
echo(commandsHTML($aCmd));	
//form 
echo("<table class='tabl01' width=\"100%\" cellpadding=\"2\" cellspacing=\"2\">\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"100%\" colspan='5'>"._t("Data-Base credentials")."</td>" .
		"</tr>\n");
	echo("<tr>" .
			"<td class=\"labl01\" width=\"20%\" colspan='1'>Data-Base type</td>" .
			"<td class=\"text01\" width=\"80%\" colspan='4'>".$aDataConPar['dbcType']."</td>" .
		"</tr>\n");
	echo("<tr>" .
			"<td class=\"labl01\" colspan='1'>Data-Base server</td>" .
			"<td class=\"text01\" colspan='4'>".$aDataConPar['host']."</td>" .
		"</tr>\n");
	echo("<tr>" .
			"<td class=\"labl01\" colspan='1'>Data-Base user</td>" .
			"<td class=\"text01\" colspan='4'>".$aDataConPar['user']."</td>" .
		"</tr>\n");
	echo("<tr>" .
			"<td class=\"labl01\" colspan='1'>Data-Base name</td>" .
			"<td class=\"text01\" colspan='4'>".$aDataConPar['dbName']."</td>" .
		"</tr>\n");
if (isset($objSync)) {
	if (sxREQUEST('reqtype') == 'syn') {
		$sCreated = '** created **';
		$sAttributeDeleted = (defined('DB_SYNC_WITH_DROP') and DB_SYNC_WITH_DROP === true) ?
				'** deleted **':'-- not defined --';
		$sIndexDeleted = '** eliminato **';
	} else {
		$sCreated = '** creating **';
		$sAttributeDeleted = (defined('DB_SYNC_WITH_DROP') and DB_SYNC_WITH_DROP === true) ?
				'** deleting **':'-- not defined --';
		$sIndexDeleted = '** deleting **';
	}
	echo("<tr>" .
			"<td class=\"titl01\" colspan='5'>"._t("New entities")."</td>" .
		"</tr>\n");
	if (count($objSync->aEntCreated) == 0) {
		echo("<tr><td class=\"text01\" colspan='5'>-- no entities --</td></tr>\n");
	} else {
		echo("<tr>");
		echo("<td class=\"tabh01\">Entity</td>");
		echo("<td class=\"tabh01\">c/i</td>");
		echo("<td class=\"tabh01\">Attribute</td>");
		echo("<td class=\"tabh01\">Data-base</td>");
		echo("<td class=\"tabh01\">Meta-base</td>");
		echo("</tr>\n");
		foreach($objSync->aEntCreated as $ent => $aX) {
			$entX = $ent;
			foreach($aX['a'] as $attr => $car) {
				echo("<tr>");
				echo("<td class=\"text01\">$entX</td>");
				echo("<td class=\"text01\">&nbsp;</td>");
				echo("<td class=\"text01\">$attr</td>");
				echo("<td class=\"text01\">$sCreated</td>");
				echo("<td class=\"text01\">$car</td>");
				echo("</tr>\n");
				$entX = '&nbsp;';
			}
			foreach($aX['i'] as $attr => $car) {
				echo("<tr>");
				echo("<td class=\"text01\">$entX</td>");
				echo("<td class=\"text01\">(ind)</td>");
				echo("<td class=\"text01\">$attr</td>");
				echo("<td class=\"text01\">$sCreated</td>");
				echo("<td class=\"text01\">$car</td>");
				echo("</tr>\n");
				$entX = '&nbsp;';
			}
		}
	}
	echo("<tr>" .
			"<td class=\"titl01\" width=\"100%\" colspan='5'>"._t("Modified entities ")."</td>" .
		"</tr>\n");
	if (count($objSync->aEntAltered) == 0) {
		echo("<tr><td class=\"text01\" colspan='5'>-- no entities --</td></tr>\n");
	} else {
		echo("<tr>");
		echo("<td class=\"tabh01\">Entity</td>");
		echo("<td class=\"tabh01\">c/i</td>");
		echo("<td class=\"tabh01\">Attribute</td>");
		echo("<td class=\"tabh01\">Data-base</td>");
		echo("<td class=\"tabh01\">Meta-base</td>");
		echo("</tr>\n");
		dbg_("objSync->aEntAltered=".print_r($objSync->aEntAltered,1));
		foreach($objSync->aEntAltered as $ent => $aX) {
			$entX = $ent;
			if (isset($aX['a'])) {
				foreach($aX['a'] as $attr => $aCar) {
					echo("<tr>");
					echo("<td class=\"text01\">$entX</td>");
					echo("<td class=\"text01\">&nbsp;</td>");
					echo("<td class=\"text01\">$attr</td>");
					echo("<td class=\"text01\">".((isset($aCar['old']))?$aCar['old']:$sCreated)."</td>");
					echo("<td class=\"text01\">".((isset($aCar['new']))?$aCar['new']:$sAttributeDeleted)."</td>");
					echo("</tr>\n");
					$entX = '&nbsp;';
				}
			}
			if (isset($aX['i'])) {
				foreach($aX['i'] as $attr => $aCar) {
					echo("<tr>");
					echo("<td class=\"text01\">$entX</td>");
					echo("<td class=\"text01\">(ind)</td>");
					echo("<td class=\"text01\">$attr</td>");
					echo("<td class=\"text01\">".((isset($aCar['old']))?$aCar['old']:$sCreated)."</td>");
					echo("<td class=\"text01\">".((isset($aCar['new']))?$aCar['new']:$sIndexDeleted)."</td>");
					echo("</tr>\n");
					$entX = '&nbsp;';
				}
			}
		}
	}
	if (count($objSync->aEntDeleted) > 0) {
		echo("<tr>" .
				"<td class=\"titl01\" width=\"100%\" colspan='5'>");
		if (!defined('DB_SYNC_WITH_DROP') or DB_SYNC_WITH_DROP !== true) {
				echo _t("Entities not in meta-base - delete disabled");
		} elseif(sxREQUEST('reqtype') == 'syn') {
				echo _t("Entities not in meta-base - DROPPED !");
		} else {
				echo _t("Entities not in meta-base - ATTENTION, will be dropped !");
		}
		echo("</td>" .
			"</tr>\n");
		echo("<tr>");
		echo("<td class=\"tabh01\">Entities</td>");
		echo("<td class=\"tabh01\">c/i</td>");
		echo("<td class=\"tabh01\">Attribute</td>");
		echo("<td class=\"tabh01\">Data-base</td>");
		echo("<td class=\"tabh01\">Meta-base</td>");
		echo("</tr>\n");
		foreach($objSync->aEntDeleted as $ent => $aX) {
			$entX = $ent;
			foreach($aX as $attr => $car) {
				echo("<tr>");
				echo("<td class=\"text01\">$entX</td>");
				echo("<td class=\"text01\">&nbsp;</td>");
				echo("<td class=\"text01\">$attr</td>");
				echo("<td class=\"text01\">$car</td>");
				echo("<td class=\"text01\">&nbsp;</td>");
				echo("</tr>\n");
				$entX = '&nbsp;';
			}
		}
	}
}		
echo("</table>");
echo(commandsHTML($aCmd));
echo(footerHTML());
